<template>
    <div>
        <a-modal
            :visible="applicationFormVisible"
            width="100%"
            wrapClassName="form-modal-Lu"
            @cancel="closeVisible"
        >
            <a-tabs type="card" v-if="applicationFormVisible">
                <a-tab-pane key="1" tab="预约表">
                    <div id="printTest" class="print-div-shu"  style="height: auto;;">
                        <div style="text-align:center;">
                            <h1>xx食品药品审评查验中心现场咨询预约表</h1>
                        </div>
                        <a-form :form="form" >
                            <table :key="productsDevice" border="1" class="table-Lu consultationAppointment-modal-table">
                                <tr>
                                    <th >预约日期</th>
                                    <td colspan="3">{{ djbData.fyyrq }}</td>
                                </tr>
                                <tr>
                                    <th >咨询事项类别</th>
                                    <td colspan="3">
                                        <div v-for="(item,index) in typeList" :key="index" class="typeCheck-div">
                                            <div class="typeCheck-fjDiv">
                                                <a-checkbox :checked="item.fj.fxzzt == '0'?false:true" disabled style="font-weight: 600;">
                                                    {{ item.fj.fmc}}
                                                </a-checkbox>
                                            </div>
                                            <div>
                                                <a-checkbox
                                                    v-for="(items,indexs) in item.zjList"
                                                    :key="indexs"
                                                    :checked="items.fxzzt == '0'?false:true"
                                                    disabled>
                                                    {{ items.fmc}}
                                                </a-checkbox>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th >申请企业名称</th>
                                    <td>{{ djbData.fsqqymc }}</td>
                                    <th>计划来访人数</th>
                                    <td>{{ djbData.fjhlfrs }}</td>
                                </tr>
                                <tr>
                                    <th>咨询联系人</th>
                                    <td>{{ djbData.fzxlxr }}</td>
                                    <th>号码</th>
                                    <td>{{ djbData.fsj }}</td>
                                </tr>
                                <tr>
                                    <th>主要咨询问题</th>
                                    <td colspan="3">
                                        <p class="text-align-left">{{ djbData.fzyzxwt }}</p>
                                    </td>
                                </tr>
                                <tr>
                                    <th>备注</th>
                                    <td colspan="3">
                                        <p class="text-align-left">{{ djbData.fbz }}</p>
                                    </td>
                                </tr>
                                <tr v-if="linkName == '综合科审核'">
                                    <th>质量管理与科研科审核</th>
                                    <td colspan="3">
                                        <a-form-item>
                                            <a-select
                                                style="width: 100%;"
                                                placeholder="请选择分发科室"
                                                v-model:value="formState.dbrid"
                                                v-decorator="[
                                                    'dbrid', { initialValue:formState.dbrid, rules: rules.dbrid}
                                                ]"
                                                >
                                                <a-select-option v-for="(item,index) in deptList" :value="item.id" :key="index">
                                                    {{ item.orgName + '-' + item.realname }}
                                                </a-select-option>
                                            </a-select>
                                        </a-form-item>
                                    </td>
                                </tr>
                                <tr>
                                    <th>质量管理与科研科意见</th>
                                    <td colspan="3" v-if="linkName == '综合科审核'">
                                        <a-form-item>
                                            <reviewOpinions
                                                v-model="formState.fzhkshyj"
                                                ref="reviewOpinionsRefZh"
                                                @getInputOpinion="getInputOpinion"
                                                @openOpinions="zhOpinions"
                                                v-decorator="[
                                                    'fzhkshyj', { initialValue:formState.fzhkshyj, rules: rules.fzhkshyj}
                                                ]"/>
                                        </a-form-item>
                                    </td>
                                    <td v-else colspan="3">
                                        <p class="text-align-left">{{ djbData.fzhkshyj }}</p>
                                        <div class="yjqm-right-bottom">
                                            <img v-if="getUrl('综合科审核') && djbData.fzhkshsj" :src="getUrl('综合科审核')" alt="" class="yjqmImg"/>
                                            <span v-else class="yjqmImg">{{ djbData.fzhkshr }}</span>
                                            <span>{{ djbData.fzhkshsj }}</span>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th>经办科室审核</th>
                                    <td colspan="3" v-if="linkName == '经办科室审核'">
                                        <a-form-item>
                                            <reviewOpinions
                                              ref="reviewOpinionsRefKs"
                                              @getInputOpinion="getInputOpinion"
                                              @openOpinions="ksOpinions"
                                              v-decorator="[
                                                    'fksshyj', { initialValue:formState.fksshyj, rules: rules.fksshyj}
                                                ]"/>
                                        </a-form-item>
                                    </td>
                                    <td v-else colspan="3">
                                        <p class="text-align-left">{{ djbData.fksshyj }}</p>
                                        <div class="yjqm-right-bottom">
                                            <img v-if="getUrl('经办科室审核') && djbData.fksshsj" :src="getUrl('经办科室审核')" alt="" class="yjqmImg"/>
                                            <span v-else class="yjqmImg">{{ djbData.fksshr }}</span>
                                            <span>{{ djbData.fksshsj }}</span>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th>回复咨询人</th>
                                    <td colspan="3" v-if="linkName == '回复咨询人'" style="padding-left:20px;">
                                        <a-form-item label="接待会议室" style="display: flex;">
                                            <a-input
                                              v-model="formState.fjdhys"
                                              style="width: 100%;"
                                              placeholder="请输入接待会议室"
                                              v-decorator="[
                                                  'fjdhys', { initialValue:formState.fjdhys, rules: rules.fjdhys}
                                              ]"/>
                                        </a-form-item>
                                        <a-form-item label="咨询时段" style="display: flex;">
                                            <a-time-picker format="HH:mm" :minute-step="10" @change="onChange($event,0)" :disabledHours="disabledHours"
                                            v-decorator="[
                                            'startTime', { initialValue:formState.startTime, rules: rules.startTime}
                                        ]"/>
                                            -至-
                                            <a-time-picker format="HH:mm" :minute-step="10" @change="onChange($event,1)" :disabledHours="disabledHours"
                                            v-decorator="[
                                            'endTime', { initialValue:formState.endTime, rules: rules.endTime}
                                        ]"/>
                                        </a-form-item>
                                    </td>
                                    <td v-else-if="djbData.fjdhys" colspan="3" style="text-align:left;padding-left:10px;">
                                      <p style="margin-bottom: 0;">接待会议室:{{djbData.fjdhys}}</p>
                                      <p style="margin-bottom: 0;">咨询时段: {{djbData.fzxsd}}</p>
                                    </td>
                                    <td v-else colspan="3"></td>
                                </tr>
                            </table>
                        </a-form>
                    </div>
                </a-tab-pane>
                <a-button
                    slot="tabBarExtraContent"
                    v-print="printObj"
                    class="print-btn">
                    打印
                </a-button>
            </a-tabs>
            <template slot="footer">
                <div style="background:#fff;" v-if="linkName == '综合科审核'">
                    <a-button class="blue-btn" @click="saveFuntion">保存</a-button>
                    <a-button class="blue-btn" @click="zhkShenhe('1')">审核通过</a-button>
                    <a-button class="red-btn" @click="zhkShenhe('0')">审核不通过</a-button>
                </div>
                <div style="background:#fff;" v-else-if="linkName == '经办科室审核'">
                    <a-button class="blue-btn" @click="ksHandle(1)">受理</a-button>
                    <a-button class="red-btn" @click="returnHandle">退回</a-button>
                </div>
                <div style="background:#fff;" v-else-if="linkName == '回复咨询人'">
                    <a-button class="blue-btn" @click="hfHandle">审核通过</a-button>
                </div>
                <div v-else>
                    <a-button @click="applicationFormVisible = false" type="primary" style="margin-right: 10px;">关闭</a-button>
                </div>
            </template>
        </a-modal>
    </div>
</template>
<script>
import { sxyybFuntion,querySpzxDeptList,queryJobByJobId,saveFlow,submitFlow,
        queryAllYbJob,returnUpperzdyFlowJob,sendmsg,shfail } from './service/index'
import moment from 'moment'
export default {
    components:{

    },
    props:{
        linkName:{
            type:String,
            default:'',
            required:false,
        },
        formData:{
            type:[Array,Object],
            default: () => {},
            required:false
        },
    },
    data(){
        return{
            moment,
            djbData:{},
            applicationFormVisible:false,
            productsDevice:null,
            formState:{},
            form: this.$form.createForm(this, { name: 'coordinated' }),
            typeList: [],
            deptList: [],
            rules:{
                dbrid: [{
                    required: true,
                    message: '请选择分发科室',
                    trigger: 'blur',
                }],
                fzhkshyj: [{
                    required: true,
                    message: '请输入质量管理与科研科室审核意见',
                    trigger: 'blur',
                }],
                fjdhys: [{
                    required: true,
                    message: '请输入接待会议室',
                    trigger: 'blur',
                }],
                startTime: [{
                    type:'object',
                    required: true,
                    message: '请输入咨询时段',
                    trigger: 'change',
                }],
                endTime: [{
                    type:'object',
                    required: true,
                    message: '请输入咨询时段',
                    trigger: 'change',
                }],
            },
            imgList: [
                {fhjmc:'综合科审核',url: ''},
                {fhjmc:'经办科室审核',url: ''},
            ],
            printObj:{
                id:"printTest", //需要打印的id名称
                popTitle:"xx食品药品审评查验中心现场咨询预约表",//文档的名称
                // preview:true,//预览功能，不好用
                zIndex:'20002',
                beforeOpenCallback(vue){
                    // //console.log('打印回调',vue)
                },
                openCallback (vue) {//打开之前
                    // //console.log('打开之前',vue)
                },
                closeCallback (vue) {//关闭了打印工具
                    // //console.log('关闭了打印工具')
                },
            },
        }
    },
    watch:{

    },
    mounted(){

    },
    methods:{
        getData(val){
          this.productsDevice = Date.now()
          //console.log('已刷新----------')
            sxyybFuntion({yybId:val}).then(res=>{
                if(res.code == 200){
                    this.djbData = res.result.qyReservation
                    //console.log('this.djbData---',this.djbData)
                    this.typeList = []
                    this.typeList = res.result.lbMapList
                    this.productsDevice = Date.now()
                    this.getQuerySpzxDeptList()
                }else{
                    this.$message.warning(res.message)
                }
            })
        },
        closeVisible(){
            this.$emit('success')
            this.applicationFormVisible = false
        },
        getQuerySpzxDeptList(){
            querySpzxDeptList().then(res => {
                this.deptList = []
                res.result.forEach(item => {
                    item.userList.forEach(items => {
                        this.deptList.push(items)
                    })
                })
                //console.log(this.deptList)
            })
        },
        // 打开意见弹窗
        zhOpinions(){
            this.$refs.reviewOpinionsRefZh.getData({opinionType:'zhkIdeaType',oldVal:this.form.fzhkshyj,valueData:{hjmc:'fzhkshyj'}})
        },
        ksOpinions(){
            this.$refs.reviewOpinionsRefKs.getData({opinionType:'ksIdeaType',oldVal:this.form.fksshyj,valueData:{hjmc:'fksshyj'}})
        },
        // 意见弹窗
        getInputOpinion(val,valueData){
            //console.log(val,valueData)
            let data = this.formState
            data[valueData.hjmc] = val
            this.form.setFieldsValue({
                fzhkshyj:this.formState.fzhkshyj,
                fksshyj: this.formState.fksshyj,
            });
        },
        getUrl(ids){
            let url = ''
            this.imgList.forEach(item => {
                if(item.fhjmc == ids){
                url = item.url
                }
            })
            return url
        },
        saveFuntion(){
            let data = {
                currentNode: "",
                dbrid: "",
                jobid: this.formData.jobid,
                lcmbCode: "",
                map: this.formState,
                nextNode: "",
                nextNodeId: "",
                oaid: this.formData.oaid,
                opinion: "",
                smgflag: ""
            }
            saveFlow(data).then(res => {
                if(res.code == 200){
                    this.$message.success(res.message)
                } else {
                    this.$message.warning(res.message)
                }
            })
        },
        zhkShenhe(val){
            //console.log(this.djbData)
            let data = {
                currentNode: "",
                dbrid: "",
                jobid: this.formData.jobid,
                lcmbCode: "",
                map: {},
                nextNode: "",
                nextNodeId: "",
                oaid: this.formData.oaid,
                opinion: "",
                smgflag: ""
            }
            if(val == '1'){
                this.form.validateFields((err, values) => {
                    //console.log({...this.formState,...values,})
                    //console.log(err, values)
                    if (!err) {
                        data.dbrid = this.formState.dbrid
                        data.opinion = this.formState.fzhkshyj
                        data.map = {...this.formState,...values,'fsftgsh':val}
                        //console.log('data',data)
                        submitFlow(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                this.closeVisible()
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    } else {
                        return false;
                    }
                })
            } else if(val == '0'){
                if(this.formState.fzhkshyj){
                    // data.opinion = this.formState.fzhkshyj
                    // data.map = {'fsftgsh':val}
                    let data1 = {
                      jobid: this.formData.jobid,
                      opinion: this.formState.fzhkshyj
                    }
                    shfail(data1).then(res => {
                        if(res.code == 200){
                            this.$message.success(res.message)
                            // this.closeVisible()
                            this.sendMsg(this.formState.fzhkshyj,this.formData.oaid)
                        } else {
                            this.$message.warning(res.message)
                        }
                    })
                } else {
                    this.$message.warning('请输入审核不通过意见')
                }
            }
        },
        sendMsg(str,ids){
          let that = this
          that.$confirm({
            title:'是否发送短信？',
              onOk() {
                //console.log(str,ids)
                let data1 = {
                  id: ids,
                  opinion: str
                }
                  sendmsg(data1).then(res => {
                    //console.log('是否发送短信？',res)
                    if(res.code == 200){
                      that.$message.success(res.message)
                      that.closeVisible()
                    }
                  })
              },
              onCancel() {
                //console.log('Cancel');
                that.closeVisible()
              },
          })
        },
        // 科室审核通过
        ksHandle(val){
            let data = {
                currentNode: "",
                dbrid: "",
                jobid: this.formData.jobid,
                lcmbCode: "",
                map: this.formState,
                nextNode: "",
                nextNodeId: "",
                oaid: this.formData.oaid,
                opinion: "",
                smgflag: ""
            }
            this.form.validateFields((err, values) => {
                    //console.log({...this.formState,...values,})
                    //console.log(err, values)
                    if (!err) {
                        data.opinion = this.formState.fksshyj
                        data.map = {'fksshyj': this.formState.fksshyj}
                        //console.log('data',data)
                        submitFlow(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                this.closeVisible()
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    } else {
                        return false
                    }
            })
        },
        // 科室退回
        returnHandle(){
            if(this.formState.fksshyj){
                queryAllYbJob({jobid:this.formData.jobid}).then(res => {
                    let list = res.result
                    list.forEach(item => {
                        if(item.fhjmc == '综合科审核'){
                            this.returnFuntion(this.formData.jobid,item.id)
                        }
                    })
                })
            } else {
                this.$message.warning('请输入退回意见')
            }
        },
        returnFuntion(jobid,ids){
            let returnStr = this.formState.fksshyj
            let data = {
                jobid: jobid,
                opinion: returnStr,
                shjid: ids,
                smgflag: '',
                smsContent: '',
            }
            returnUpperzdyFlowJob(data).then(res => {
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.closeVisible()
                } else {
                    this.$message.error(res.message)
                }
            })
        },
        //
        hfHandle(){
          let data = {
                currentNode: "",
                dbrid: "",
                jobid: this.formData.jobid,
                lcmbCode: "",
                map: this.formState,
                nextNode: "",
                nextNodeId: "",
                oaid: this.formData.oaid,
                opinion: "",
                smgflag: ""
            }
            this.form.validateFields((err, values) => {
                    //console.log({...this.formState,...values,})
                    //console.log(err, values)
                    if (!err) {
                        data.opinion = ''
                        data.map = {'fjdhys': this.formState.fjdhys,'fzxsd':this.formState.fzxsd}
                        //console.log('data',data)
                        submitFlow(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                // this.closeVisible()
                                this.sendMsg('',this.formData.oaid)
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    } else {
                        return false
                    }
            })
        },
        onChange(time,type) {
          // //console.log(time,type);
          if(type == 0){
            this.formState.startTime = time?moment(time,'HH:mm'):null
            this.form.setFieldsValue({
                startTime:moment(this.formState.startTime).format('HH:mm'),
            });
            // //console.log(typeof this.formState.startTime)
          } else {
            this.formState.endTime = time?moment(time,'HH:mm'):null
            this.form.setFieldsValue({
                endTime:moment(this.formState.endTime).format('HH:mm'),
            });
          }
          if(this.formState.startTime && this.formState.endTime){
            this.formState.fzxsd = moment(this.formState.startTime).format('HH:mm') +'-'+ moment(this.formState.endTime).format('HH:mm'),
            this.form.setFieldsValue({
                fzxsd:this.formState.fzxsd
            });
          } else {
            this.form.setFieldsValue({
                fzxsd: null
            });
          }
          // //console.log('111',this.form,'222',this.formState)
        },
        disabledHours(){
          return [0,1,2,3,4,5,6,7,18,19,20,21,22,23]
        },
    }
}
</script>
<style>
@import url('../css/oaLess.css');
@import url('../css/oaClass.css');
</style>
<style lang="less">
.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }
  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
    background:#fff;
  }
  .ant-modal-body {
    flex: 1;
    background:#fff;
  }
  .ant-modal-footer{
    background:#fff;
  }
}
.consultationAppointment-modal-table {
    th {
        width: 140px !important;
    }
    td {
        font-weight: 100 !important;
    }
    .typeCheck-div {
        margin-bottom: 10px;
        line-height: 2;
        .ant-checkbox-disabled + span {
            color: black;
            padding-left: 0;
        }
        .ant-checkbox-wrapper + .ant-checkbox-wrapper {
            margin-left: 0;
        }
        div {
            display: table-cell;
            text-align: left;
            padding-left: 10px;
        }
        .typeCheck-fjDiv {
            width: 140px;
        }
    }
}
</style>
